home *** CD-ROM | disk | FTP | other *** search
Text File | 1988-06-16 | 7.2 KB | 391 lines | [TEXT/KAHL] |
- /*
- Copyright (c) 1987 by small systems guild
-
- small systems guild
- PO box 2751
- Ann Arbor, MI 48106
- (313) 996-4238
-
- File: ssgSCSI.c
- Model: LightSpeed C 2.11, Aztec C 1.06i
- Target: Macintosh Operating System
-
- #include <quickdraw.h>
- #include "ssgSCSI.h"
-
- struct SCSIInstr cmdbuf;
- struct SCSIInstr mbuf[3];
- Byte tst[10];
- int sect,stat,msg;
- Byte *cmp;
- long wait,len;
- OSErr err;
- int verbose = 0;
-
- gSC_init()
- {
- cmp = (unsigned char *)NewPtr((long)SIZE);
- }
-
-
- OSErr gSC_loadUnload(scuzzID)
- int scuzzID;
- {
- if (gSC_select(scuzzID) != noErr) {
- return(-1);
- }
- tst[0] = 0x1B;
- tst[1] = 0x01; /* return immediately after command */
- tst[2] = 0x00;
- tst[3] = 0x00;
- tst[4] = 0x00;
- tst[5] = 0x00;
- gSC_cmd(tst);
- }
-
- OSErr gSC_unitReady(scuzzID)
- int scuzzID;
- {
- if (gSC_select(scuzzID) != noErr) {
- return(-1);
- }
- tst[0] = 0x00;
- tst[1] = 0x00;
- tst[2] = 0x00;
- tst[3] = 0x00;
- tst[4] = 0x00;
- tst[5] = 0x00;
- return(gSC_cmd(tst));
- }
-
- OSErr gSC_reqSense(scuzzID)
- int scuzzID;
- {
- Byte len;
-
- if (gSC_select(scuzzID) != noErr) {
- return(-1);
- }
- gSC_CMD(0x03,NULL,NULL);
- gSC_error(SCSIRead(gSC_TIB(cmp,13L,1L)));
- gSC_finish();
- if(verbose)
- printf("request sense: %x\n",cmp[0]);
- return(stat);
- }
-
- OSErr gSC_read(scuzzID,buf,block,sector,count)
- int scuzzID;
- Byte *buf;
- Int32 block;
- Int16 sector;
- UInt16 count;
- {
- if (gSC_select(scuzzID) != noErr) {
- return(-1);
- }
- gSC_CMD(0x08,(long)((block * 32) + sector),(long)count);
- gSC_error(SCSIRead(gSC_TIB(buf,512L,(long)count)));
- gSC_finish();
- return(stat);
- }
-
- OSErr gSC_write(scuzzID,buf,block,sector,count)
- int scuzzID;
- Byte *buf;
- Int32 block;
- Int16 sector;
- UInt16 count;
- {
- if (gSC_select(scuzzID) != noErr) {
- return(-1);
- }
- gSC_CMD(0x0A,(long)((block * 32) + sector),(long)count);
- gSC_error(SCSIWrite(gSC_TIB(buf,512L,(long)count)));
- gSC_finish();
- return(stat);
- }
-
- OSErr gSC_inquire(scuzzID)
- int scuzzID;
- {
- if (gSC_select(scuzzID) != noErr) {
- return(-1);
- }
- gSC_CMD(0x12,NULL,NULL);
- gSC_error(SCSIRead(gSC_TIB(cmp,36L,1L)));
- gSC_finish();
- if(verbose)
- printf("inquiry: %s\n",&cmp[8]);
- return(stat);
- }
-
- OSErr gSC_rdCapacity(scuzzID,buf)
- int scuzzID;
- Byte *buf;
- {
- if (gSC_select(scuzzID) != noErr) {
- return(-1);
- }
- gSC_CMD(0x25,NULL,NULL);
- gSC_error(SCSIRead(gSC_TIB(buf,8L,1L)));
- gSC_finish();
- if(verbose)
- printf("SCSI ID: %d address: %ld size: %ld\n",scuzzID,*((long *)(&buf[0])),*((long *)(&buf[4])));
- return(stat);
- }
-
- OSErr gSC_cmd(cmd) /* handles 6 byte SCSI commands only */
- Byte *cmd;
- {
- gSC_error(SCSICmd(cmd,(long)6));
- return(gSC_finish());
- }
-
- OSErr gSC_6cmd(cmd)
- Byte *cmd;
- {
- gSC_error(SCSICmd(cmd,(long)6));
- }
-
- OSErr gSC_10cmd(cmd)
- Byte *cmd;
- {
- gSC_error(SCSICmd(cmd,(long)10));
- }
-
- Byte *gSC_TIB(buf,size,times)
- Byte *buf;
- long size,times;
- {
- mbuf[0].scOpcode = scInc;
- mbuf[0].scParam1 = (long)buf;
- mbuf[0].scParam2 = (long)size;
- mbuf[1].scOpcode = scLoop;
- mbuf[1].scParam1 = -10L;
- mbuf[1].scParam2 = (long)times;
- mbuf[2].scOpcode = scStop;
- mbuf[2].scParam1 = NULL;
- mbuf[2].scParam2 = NULL;
- return((Byte *)mbuf);
- }
-
- OSErr gSC_CMD(cmdID,address,sectors) /* create SCSI command block structure */
- Byte cmdID;
- long address;
- long sectors;
- {
- long cmdBytes = 6L;
-
- tst[0] = 0x00;
- tst[1] = 0x00;
- tst[2] = 0x00;
- tst[3] = 0x00;
- tst[4] = 0x00;
- tst[5] = 0x00;
- tst[6] = 0x00;
- tst[7] = 0x00;
- tst[8] = 0x00;
- tst[9] = 0x00;
-
- switch (cmdID) {
- case 0x03: /* SCSI request sense */
- tst[0] = 0x03;
- tst[4] = 0x0D; /* tell device that 13 bytes are available for data */
- break;
- case 0x08: /* SCSI read request */
- tst[0] = 0x08;
- tst[2] = (UInt16)address >> 8;
- tst[3] = (UInt16)address & 0xFF;
- tst[4] = (Byte)sectors;
- break;
- case 0x0A: /* SCSI write request */
- tst[0] = 0x0A;
- tst[2] = (UInt16)address >> 8;
- tst[3] = (UInt16)address & 0xFF;
- tst[4] = (Byte)sectors;
- break;
- case 0x12: /* SCSI inquiry */
- tst[0] = 0x12;
- tst[4] = 0x24; /* tell device that 36 bytes are available for inquery */
- break;
- case 0x25: /* Read capacity SCSI comamnd */
- tst[0] = 0x25; /* Read capacity opcode */
- tst[1] = 0x00; /* ask for last logical block address and block length */
- cmdBytes = 10L;
- break;
- case 0x2C: /* Read tape header - Irwin specific command */
- tst[0] = 0x2C;
- tst[7] = 0x04; /* set transfer size to 1024 */
- cmdBytes = 10L;
- break;
- case 0x2D: /* Write tape header - Irwin specific command */
- tst[0] = 0x2D;
- tst[7] = 0x04; /* set transfer size to 1024 */
- cmdBytes = 10L;
- break;
- }
- return(gSC_error(SCSICmd(tst,cmdBytes)));
- }
-
- OSErr gSC_select(ID)
- int ID;
- {
- gSC_error(err = SCSIGet());
- if (err != noErr)
- return(-1);
-
- gSC_error(err = SCSISelect(ID));
- if (err != noErr) {
- return(-1);
- }
- return(noErr);
- }
-
- OSErr gSC_error(errNum)
- OSErr errNum;
- {
- if (verbose) {
- switch(errNum) {
- case noErr:
- /* printf("SCSI cmd: OK\n");*/
- break;
- case -1:
- printf("SCSI error: Internal select/build error\n");
- break;
- case scCommErr:
- printf("SCSI error: Breakdown in SCSI protocols\n");
- break;
- case scPhaseErr:
- printf("SCSI error: Phase error\n");
- break;
- case scBadParmsErr:
- printf("SCSI error: Unrecognized instruction\n");
- break;
- case scCompareErr:
- printf("SCSI error: Data comparison error during compare\n");
- break;
- default:
- printf("SCSI error: Unknown error number %d\n",errNum);
- break;
- }
- }
- }
-
- OSErr gSC_finish()
- {
- wait = TIME_OUT;
- gSC_error(SCSIComplete(&stat,&msg,wait));
- if (verbose)
- printf("status: %d message: %d\n",stat,msg);
- return(stat);
- }
-
- gSC_scan(driveList,maxDrives)
- int *driveList;
- int maxDrives;
- {
- int i,j;
-
- for (j = 0;j < 2;j++) {
- for (i = 0;i < maxDrives;i++) {
- driveList[i] = 0;
- if (i == 7) {
- driveList[i] = 3;
- continue;
- }
-
- if (err = gSC_unitReady(i) != noErr) {
- if (err != 2) {
- driveList[i] = 0;
- continue;
- }
- else
- driveList[i] = 4;
- }
-
- if (gSC_inquire(i) != noErr) {
- driveList[i] = 0;
- continue;
- }
- else {
- if (cmp[8] == 'I' && cmp[9] == 'R')
- driveList[i] = 2;
- else
- driveList[i] = 1;
- }
- }
- }
- }
-
- dump_drvList(driveList,maxDrives)
- int *driveList;
- int maxDrives;
- {
- int i;
-
- printf("SCSI bus device available list\n",i);
- for (i = 0;i < maxDrives;i++) {
- switch (driveList[i]) {
- case 0:
- printf("SCSI device %d is not available\n",i);
- break;
- case 1:
- printf("SCSI device %d is a hard disk\n",i);
- break;
- case 2:
- printf("SCSI device %d is an Irwin tape drive\n",i);
- break;
- case 3:
- printf("SCSI device %d is an Apple Macintosh\n",i);
- break;
- case 4:
- printf("SCSI device %d is present but not ready\n",i);
- break;
- }
- }
- }
-
- int gSC_findTape(maxDrives)
- int maxDrives;
- {
- int i,j;
- int driveList[8], drive = -1;
-
- for (j = 0;j < 2;j++) {
- for (i = 0;i < maxDrives;i++) {
- driveList[i] = 0;
- if (i == 7) {
- driveList[i] = 3;
- continue;
- }
-
- if (err = gSC_unitReady(i) != noErr) {
- if (err != 2) {
- driveList[i] = 0;
- continue;
- }
- else
- driveList[i] = 4;
- }
-
- if (gSC_inquire(i) != noErr) {
- driveList[i] = 0;
- continue;
- }
- else {
- if (cmp[8] == 'I' && cmp[9] == 'R')
- driveList[i] = 2;
- else
- driveList[i] = 1;
- }
- }
- }
- for (i = 0;i < maxDrives;i++) {
- if (driveList[i] == 2)
- drive = i;
- }
- return(drive);
- }
-
-